//
//  BankDetailViewController.m
//  51易借
//
//  Created by apple on 2017/9/20.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "BankDetailViewController.h"
#import "MemberInfoModel.h"
#import "BankManagerViewController.h"
@interface BankDetailViewController ()
@property (nonatomic,strong)MemberInfoModel * member;
@end

@implementation BankDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACK_COLOR;
    
    
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.view).offset(10);
//        make.top.mas_equalTo(self.view).offset(25);
        make.leading.mas_equalTo(self.iconImageV);
        make.centerY.mas_equalTo(self.iconImageV);
    }];
//    self.nameLabel.textColor = [UIColor redColor];
    
    [self setupNavBar];
    [self setupUI];
}


- (void)setupNavBar{
    
    self.title = @"设置";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    self.title = @"银行卡信息";
    
    self.topView.backgroundColor = TOTAL_COLOR;
    
    self.iconImageV.hidden = YES;
    
//    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.top.mas_equalTo(self.view);
//        make.height.mas_equalTo(64 * SCREENH6);
//    }];
}




- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getPageModel];
}
- (IBAction)changeTask:(UIButton *)sender {
    HIDDEN_TAB;
    BankManagerViewController* vc = [BankManagerViewController new];
//    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)getPageModel
{
    ParaModel*para = [ParaModel new];
    
    
    [[NetworkTool shareManager] POST:[Interfacefile loadCurrentPersonalInfo] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            self.member = [[MemberInfoModel alloc] initWithDictionary:[responseObject objectForKey:@"data"] error:nil];
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    
}


-(void)setMember:(MemberInfoModel *)member
{
    _member = member;
    self.nameLabel.text = member.bank;
    self.mobileLabel.text = [self securityNumber:member.bankTel];
    self.cardLabel.text = [self getNewBankNumWitOldBankNum:member.bankCard];
}
-(NSString *)getNewBankNumWitOldBankNum:(NSString *)bankNum
{
    NSMutableString *mutableStr;
    if (bankNum.length) {
        mutableStr = [NSMutableString stringWithString:bankNum];
        for (int i = 0 ; i < mutableStr.length; i ++) {
            if (i>2&&i<mutableStr.length - 3) {
                [mutableStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
            }
        }
        NSString *text = mutableStr;
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *newString = @"";
        while (text.length > 0) {
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 4) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        return newString;
    }
    return bankNum;
    
}

-(NSString*)securityNumber:(NSString *)number
{
    if(number.length==11)
    {
        number = [number stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    }
    return number;
}


@end
